package com.libraryWeb.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.libraryWeb.dao.AdminDao;
import com.libraryWeb.entity.Admin;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	AdminDao adminDao = new AdminDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//统一设置编码处理
		request.setCharacterEncoding("utf-8");// post
		response.setContentType("text/html;charset=utf-8");
		
		//获取当前的动作
		String action = request.getParameter("action");
		if("register".equals(action)) {
			register(request,response);
		}else if("login".equals(action)) {
			login(request,response);
		}else if("exit".equals(action)) {
			exit(request,response);
		}
		
	}
	
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		   String msg = null;  
		  //1、获取页面提交的参数
		  System.out.println("method = "+request.getMethod());//
		  
		  String userName = request.getParameter("userName");
		  String pwd = request.getParameter("pwd");
		  
		  System.out.println("userName = "+userName);
		  System.out.println("pwd = "+pwd);
		  AdminDao adminDao = new AdminDao();
		  Admin admin = adminDao.query(userName, pwd);
		  if(admin != null) {
			  //request.setAttribute("admin", admin);
			  HttpSession session = request.getSession();//有的话，直接拿，没有的话，就去创建
			  session.setAttribute("admin", admin);
			  //设置Session的有效时间   sessionid
			  System.out.println(session.getId());
			  Cookie c = new Cookie("JSESSIONID",session.getId());
			  c.setMaxAge(30*60);
			  response.addCookie(c);
			  
			  //登录成功，跳到主页  home.jsp
			  request.getRequestDispatcher("/book?action=qryall").forward(request, response);
			  //request.getRequestDispatcher("/home.jsp").forward(request, response);
		  }else {
			  //登录失败
			  msg = "登录失败";
			  request.setAttribute("msg", msg);//
			  //
			  request.getRequestDispatcher("/login.jsp").forward(request, response);
		  }
	}
	
	protected void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  //String msg = null;  
		  HttpSession session = request.getSession(false);//有的话，直接拿，没有的话，则不创建，返回空
		  if(session != null) {
			  session.removeAttribute("admin");
			  //session.invalidate();//
		  }
		  
		  //跳转到登录页面
		  response.sendRedirect(request.getContextPath()+"/login.jsp");
	}

	
	protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String userName = request.getParameter("userName");
		 String pwd = request.getParameter("pwd");
		 String pwds = request.getParameter("pwds");
		 String msg = null;
		 Admin admin = new Admin();
		 admin.setUserName(userName);
		 admin.setPwd(pwds);
		  //
		  if(pwd != null && pwd.equals(pwds)) {
			  int flag = adminDao.insert(admin);
			  if(flag == 1) {
				  msg = "注册成功！";
			  }else {
				  msg = "注册失败！";
			  }
		  }else {
			  msg = "密码不一致！";
		  }
		  //
		  request.setAttribute("msg", msg);
		  request.getRequestDispatcher("/login.jsp").forward(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
